<template>
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-card shadow="hover" class="mgb20" style="height:252px;">
            <div class="user-info">
              <img src="@/assets/img/logo.jpg" class="user-avator" alt/>
              <div class="user-info-cont">
                <div class="user-info-name">您好，{{ name }}</div>
                <div style="margin-top: 10px">{{ role }}</div>
              </div>
            </div>
<!--            <div class="user-info-list">-->
<!--              上次登录时间：-->
<!--              <span>2020-8-26</span>-->
<!--            </div>-->
<!--            <div class="user-info-list">-->
<!--              上次登录地点：-->
<!--              <span>成都</span>-->
<!--            </div>-->
          </el-card>
        </el-col>
        <el-col :span="16">
          <el-row>
            <el-col :span="15" :body-style="{marginBottom: '50px'}" class="mgb45">
              <el-card shadow="hover" :body-style="{padding: '0px'}">
                <div class="grid-content grid-con-0">
                  <i class="el-icon-bank-card grid-con-icon"></i>
                  <div class="" style="margin-left: 12%;">
                    <span style="font-size: 38px;font-weight: bold">账户当前余额</span>
                    <span class="grid-num" style="margin-left: 10px;font-size: 38px;"><span style="color: #F56C6C">{{ nowCount }}</span> 元</span>
                  </div>
                </div>
              </el-card>
            </el-col>
          </el-row>
          <el-row :gutter="20" class="mgb20">
            <el-col :span="8">
              <el-card shadow="hover" :body-style="{padding: '0px'}">
                <div class="grid-content grid-con-1">
                  <i class="el-icon-lx-people grid-con-icon"></i>
                  <div class="grid-cont-right">
                    <div class="grid-num">{{ AmountCount.totalAmount / 100.0.toFixed(2) }} 元</div>
                    <div class="grid-text">转账总金额</div>
                  </div>
                </div>
              </el-card>
            </el-col>
            <el-col :span="8">
              <el-card shadow="hover" :body-style="{padding: '0px'}">
                <div class="grid-content grid-con-2">
                  <i class="el-icon-lx-goods grid-con-icon"></i>
                  <div class="grid-cont-right">
                    <div class="grid-num">{{ AmountCount.successCount }} 笔</div>
                    <div class="grid-text">成功转账</div>
                  </div>
                </div>
              </el-card>
            </el-col>
            <el-col :span="8">
              <el-card shadow="hover" :body-style="{padding: '0px'}">
                <div class="grid-content grid-con-3">
                  <i class="el-icon-lx-notice grid-con-icon"></i>
                  <div class="grid-cont-right">
                    <div class="grid-num">{{ AmountCount.failCount }} 笔</div>
                    <div class="grid-text">失败转账</div>
                  </div>
                </div>
              </el-card>
            </el-col>
          </el-row>
        </el-col>
      </el-row>
    </el-card>
</template>

<script>
import {orderAmount} from "@/api/order";
import {getPayAvailable} from "@/api/user";
import {mapGetters} from "vuex";

export default {
  name: 'Dashboard',
  data() {
    return {
      // 转账金额总显示
      AmountCount: {
        successCount : "0",
        failCount: "0",
        totalAmount : "0",
      },
      nowCount: "0",
      todoList: [
        {
          title: '今天要修复100个bug',
          status: false
        },
        {
          title: '今天要修复100个bug',
          status: false
        },
        {
          title: '今天要写100行代码加几个bug吧',
          status: false
        },
        {
          title: '今天要修复100个bug',
          status: false
        },
        {
          title: '今天要修复100个bug',
          status: true
        },
        {
          title: '今天要写100行代码加几个bug吧',
          status: true
        }
      ],
      data: [
        {
          name: '2018/09/04',
          value: 1083
        },
        {
          name: '2018/09/05',
          value: 941
        },
        {
          name: '2018/09/06',
          value: 1139
        },
        {
          name: '2018/09/07',
          value: 816
        },
        {
          name: '2018/09/08',
          value: 327
        },
        {
          name: '2018/09/09',
          value: 228
        },
        {
          name: '2018/09/10',
          value: 1065
        }
      ],
      options: {
        type: 'bar',
        title: {
          text: '最近一周各品类销售图'
        },
        xRorate: 25,
        labels: ['周一', '周二', '周三', '周四', '周五'],
        datasets: [
          {
            label: '家电',
            data: [234, 278, 270, 190, 230]
          },
          {
            label: '百货',
            data: [164, 178, 190, 135, 160]
          },
          {
            label: '食品',
            data: [144, 198, 150, 235, 120]
          }
        ]
      },
      options2: {
        type: 'line',
        title: {
          text: '最近几个月各品类销售趋势图'
        },
        labels: ['6月', '7月', '8月', '9月', '10月'],
        datasets: [
          {
            label: '家电',
            data: [234, 278, 270, 190, 230]
          },
          {
            label: '百货',
            data: [164, 178, 150, 135, 160]
          },
          {
            label: '食品',
            data: [74, 118, 200, 235, 90]
          }
        ]
      }
    };
  },
  computed: {
    ...mapGetters([
      'userId',
      'name'
    ]),
    role() {
      return this.userId === 1 ? '超级管理员' : '普通用户';
    }
  },
  // created() {
  //     this.handleListener();
  //     this.changeDate();
  // },
  // activated() {
  //     this.handleListener();
  // },
  // deactivated() {
  //     window.removeEventListener('resize', this.renderChart);
  //     bus.$off('collapse', this.handleBus);
  // },
  methods: {
    changeDate() {
      const now = new Date().getTime();
      this.data.forEach((item, index) => {
        const date = new Date(now - (6 - index) * 86400000);
        item.name = `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`;
      });
    },
    // 获取当日转账金额
    getNowAmount() {
      orderAmount.post().then(res => {
        if(res.code !== 1000) {
          this.$message.error("获取失败!");
          return;
        }
        this.AmountCount = res.data;
      });
    },
    getNowAmountCount() {
      getPayAvailable.get().then(res => {
        if(res.code !== 1000) {
          this.$message.error("获取失败!");
          return;
        }
        this.nowCount = res.data
      });
    },
    // handleListener() {
    //     bus.$on('collapse', this.handleBus);
    //     // 调用renderChart方法对图表进行重新渲染
    //     window.addEventListener('resize', this.renderChart);
    // },
    // handleBus(msg) {
    //     setTimeout(() => {
    //         this.renderChart();
    //     }, 200);
    // },
    // renderChart() {
    //     this.$refs.bar.renderChart();
    //     this.$refs.line.renderChart();
    // }
  },
  created() {
    this.getNowAmount();
    this.getNowAmountCount();
  }
};
</script>


<style scoped>
.el-row {
  margin-bottom: 20px;
}

.grid-content {
  display: flex;
  align-items: center;
  height: 100px;
}

.grid-cont-right {
  flex: 1;
  text-align: center;
  font-size: 14px;
  color: #999;
}
.grid-text {
  font-weight: bold;
}
.grid-num {
  font-size: 30px;
  font-weight: bold;
}
.mgb45 {
  margin-bottom: 45px;
  margin-left: 113px;
}
.grid-con-icon {
  font-size: 50px;
  width: 100px;
  height: 100px;
  text-align: center;
  line-height: 100px;
  color: #fff;
}
.grid-con-0 .grid-con-icon {
  background: #f1c40f;
}
.grid-con-1 .grid-con-icon {
  background: rgb(45, 140, 240);
}

.grid-con-1 .grid-num {
  color: rgb(45, 140, 240);
}

.grid-con-2 .grid-con-icon {
  background: rgb(100, 213, 114);
}

.grid-con-2 .grid-num {
  color: rgb(45, 140, 240);
}

.grid-con-3 .grid-con-icon {
  background: rgb(242, 94, 67);
}

.grid-con-3 .grid-num {
  color: rgb(242, 94, 67);
}

.user-info {
  display: flex;
  align-items: center;
  padding-bottom: 20px;
  margin-bottom: 20px;
}

.user-avator {
  width: 120px;
  height: 120px;
  border-radius: 50%;
}

.user-info-cont {
  padding-left: 50px;
  flex: 1;
  font-size: 14px;
  color: #999;
}

.user-info-cont div:first-child {
  font-size: 30px;
  color: #222;
}

.user-info-list {
  font-size: 14px;
  color: #999;
  line-height: 25px;
}

.user-info-list span {
  margin-left: 70px;
}

.mgb20 {
  margin-bottom: 20px;
}

.todo-item {
  font-size: 14px;
}

.todo-item-del {
  text-decoration: line-through;
  color: #999;
}

.schart {
  width: 100%;
  height: 300px;
}
</style>
<style>
.el-card.is-always-shadow {
  box-shadow: none;
  border: none;
}
</style>